package homework;

import java.util.Arrays;

//作业标题(983)
//3.合并两个有序数组
//
//作业内容
//3.合并两个有序数组
//https://leetcode.cn/problems/merge-sorted-array/description/
public class h983 {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1, p2 = n - 1;
        int tar = m + n - 1;
        while(p1 >= 0 || p2 >= 0)
        {
            if(p1 == -1)
            {
                nums1[tar] = nums2[p2];
                p2--;
            }
            else if(p2 == -1)
            {
                nums1[tar] = nums1[p1];
                p1--;
            }
            else if(nums1[p1] > nums2[p2])
            {
                nums1[tar] = nums1[p1];
                p1--;
            }
            else
            {
                nums1[tar] = nums2[p2];
                p2--;
            }
            tar--;
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1,2,3,0,0,0};
        int m = 3;
        int[] nums2 = {2,5,6};
        int n = 3;
        new h983().merge(nums1,m,nums2,n);
        System.out.println(Arrays.toString(nums1));
    }
}
